<template>
  <div class="issue">
    <!-- 搜索 -->
    <div class="search">
      <el-input v-model="query.question" placeholder="请输入问题" clearable />
      <el-button type="primary" icon="el-icon-search" @click="handleSearch">查找</el-button>
      <el-button type="primary" icon="el-icon-edit" @click="handAdd">添加</el-button>
      <el-button type="primary" icon="el-icon-download" @click="exportClues">导出</el-button>
    </div>
    <!-- 表单 -->
    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="id" label="问题ID" sortable width="150" />
      <el-table-column prop="question" label="问题内容" width="347" />
      <el-table-column prop="answer" label="问题回复" width="594" />
      <el-table-column fixed="right" label="操作" min-width="250">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="handeEdite(scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <div class="page">
      <el-pagination
        :page-sizes="[10, 20, 30, 40, 50, 100]"
        :page-size="this.query.limit"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
    <!-- 模态框 -->
    <el-dialog :title="title" :visible.sync="dialogVisible" width="50%">
      <div class="conter">
        <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="100px" class="demo-ruleForm">
          <el-form-item label="问题" prop="question">
            <el-input v-model=" ruleForm.question" />
          </el-form-item>
          <el-form-item label="回复" prop="answer">
            <el-input v-model="ruleForm.answer" type="textarea" style="width: 320px;" placeholder="请输入内容" />
          </el-form-item>
          <div style="float: right;">
            <el-button @click="dialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="submitForm('ruleForm')">确定</el-button>
          </div>
        </el-form>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { listIssue, createIssue, updateIssue, deleteIssue } from '@/api/issue'
import * as XLSX from 'xlsx'
export default {
  data() {
    return {
      // 参数
      query: {
        page: 1,
        limit: 20,
        sort: 'add_time',
        order: 'desc',
        question: ''
      },
      tableData: [], // / 列表数据
      total: null, // 当前页总数
      dialogVisible: false, // 模态框
      title: '', // 标题
      // 确定
      ruleForm: {
        question: null,
        answer: null
      },
      // 表单验证
      rules: {
        question: [{ required: true, message: '关键词不能为空', trigger: 'blur' }],
        answer: [{ required: true, message: '回复不能为空', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    // 渲染列表
    getList() {
      listIssue(this.query).then(res => {
        console.log('通用问题', res)
        this.tableData = res.data.data.list
        this.total = res.data.data.total
      })
    },
    // 添加
    handAdd() {
      this.dialogVisible = true
      this.title = '创建'
    },
    // 编辑
    handeEdite(row) {
      console.log(row)
      this.dialogVisible = true
      this.ruleForm = JSON.parse(JSON.stringify(row))
    },
    // 确定
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.title == '创建') {
            createIssue(this.ruleForm).then(res => {
              console.log('创建成功', res)
              this.dialogVisible = false
              this.getList()
              this.$notify({
                title: '成功',
                message: '创建成功',
                type: 'success'
              })
            })
          } else {
            updateIssue(this.ruleForm).then(res => {
              console.log('编辑成功', res)
              this.dialogVisible = false
              this.getList()
              this.$notify({
                title: '成功',
                message: '更新成功',
                type: 'success'
              })
            })
          }
        }
      })
    },
    // 删除
    handleDel(row) {
      console.log(row)
      deleteIssue(row).then(res => {
        console.log('删除成功', res)
        this.getList()
        this.$notify({
          title: '成功',
          message: '删除成功',
          type: 'success'
        })
      })
    },
    // 分页器
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
      this.query.limit = val
      this.getList()
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.query.page = val
      this.getList()
    },
    // 导出
    exportClues() {
      // 点击导出的时候把现有数据生成一个新的数组 以中文作为key然后在导出
      const demo = []
      for (let i = 0; i < this.tableData.length; i++) {
        const obje = {
          问题ID: this.tableData[i].id,
          问题内容: this.tableData[i].question,
          问题回复: this.tableData[i].answer
        }
        demo.push(obje)
      }
      console.log(demo)
      const worksheet = XLSX.utils.json_to_sheet(demo) // 这里面传入要导出的表格数据
      const workbook = XLSX.utils.book_new()
      XLSX.utils.book_append_sheet(workbook, worksheet, 'data') // data为自定义表格名字
      XLSX.writeFile(workbook, '常见问题方法.xlsx') // one.xlsx指的是文件名
    },
    // 搜索
    handleSearch() {
      this.getList()
    },
    // 重置
    resetForm(formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>
<style scoped lang="scss">
  ::v-deep.issue {
    padding: 20px;

    .search {
      display: flex;
      margin-bottom: 20px;
      background-color: re;

      .el-input {
        width: 200px;
      }
    }

    .el-textarea__inner {
      min-height: 201.3333px !important;
    }

    .el-table .cell {
      text-align: center;

    }

    .cell {
      img {
        width: 50px;
        border-radius: 25px;
      }

      .yanse {
        background-color: #e8f4ff;
        border-color: #d1e9ff;
        color: #1890ff;
        padding: 5px;
        border-radius: 4px;
        font-size: 12px;
      }

    }

    .conter {
      padding: 30px 20px;
    }

    .el-dialog {

      .el-input {
        width: 50%;
      }

      .el-form-item__label {
        text-align: left;
      }
    }

    .el-input--suffix .el-input__inner {
      margin-right: 200px;
    }

    .el-button--text {
      color: #fff;
      display: inline-block;
      padding: 8px 13px;
      background-color: #1890ff;
    }

    .page {
      margin-top: 50px;
    }

    .el-table__body,
    .el-table__header {
      width: 100% !important;

    }

  }
</style>
